package com.javenwong.android.engine;

import android.graphics.Canvas;
import android.view.MotionEvent;

public abstract class View {

	public static GameCanvas canvas;

	private boolean hasinit;

	private View parentView;
	private View childView;
	
	private boolean destroy;

	public synchronized void doDraw(Canvas g) {
		
		if (destroy) return;
		
		checkInit();

		// 子视图 绘制在 父视图 上方
		draw(g);
		if (childView != null) {
			childView.doDraw(g);
		}
	}

	private void checkInit() {
		if (!hasinit) {
			init();

			hasinit = true;
		}
	}

	public final void doLogic() {
		checkInit();

		// 包含子视图时 子视图处理逻辑，父视图不处理
		if (childView != null) {
			childView.doLogic();
		} else {
			// 视图处理逻辑
			logic();
		}
	}

	public void doTouchEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// 包含子视图时 子视图处理触屏，父视图不处理
		if (childView != null) {
			childView.doTouchEvent(event, xF, yF, x, y);
		} else {
			// 视图处理逻辑
			onTouchEvent(event, xF, yF, x, y);
		}
	}

	public void doTouchDownEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// 包含子视图时 子视图处理触屏，父视图不处理
		if (childView != null) {
			childView.doTouchDownEvent(event, xF, yF, x, y);
		} else {
			// 视图处理逻辑
			onTouchDownEvent(event, xF, yF, x, y);
		}
	}

	public void doTouchUpEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// 包含子视图时 子视图处理触屏，父视图不处理
		if (childView != null) {
			childView.doTouchUpEvent(event, xF, yF, x, y);
		} else {
			// 视图处理逻辑
			onTouchUpEvent(event, xF, yF, x, y);
		}
	}

	public void doTouchMoveEvent(MotionEvent event, float xF, float yF, int x, int y) {
		// 包含子视图时 子视图处理触屏，父视图不处理
		if (childView != null) {
			childView.doTouchMoveEvent(event, xF, yF, x, y);
		} else {
			// 视图处理逻辑
			onTouchMoveEvent(event, xF, yF, x, y);
		}
	}

	public synchronized void doDestroy() {
		destroy = true;
		destroy();

		if (childView != null)
			childView.doDestroy();

		System.gc();
	}

	/**
	 * 逻辑 抽象方法
	 * 
	 * @param key
	 * @param x
	 * @param y
	 */
	public abstract void logic();

	/**
	 * 绘制 抽象方法
	 * 
	 * @param g
	 */
	public abstract void draw(Canvas g);

	/**
	 * 获取子视图
	 * 
	 * @return
	 */
	public final View getChildView() {
		return childView;
	}

	/**
	 * 清除子视图
	 */
	public final void clearChildView() {
		childView.doDestroy();

		childView = null;

		// KeyManager.setTopView(this);
	}

	/**
	 * 添加子视图
	 * 
	 * @param view
	 */
	public final void appendView(View view) {
		if (childView == null) {
			childView = view;
			childView.parentView = this;

			// KeyManager.setTopView(childView);

		} else {
			childView.appendView(view);
		}
	}

	public View getParentView() {
		return parentView;
	}

	public void setParentView(View parentView) {
		this.parentView = parentView;
	}

	public void setRightNum(byte num) {
	}

	/**
	 * 返回
	 */
	public void back(){
		
	}
	
	protected abstract void init();

	protected abstract void destroy();

	public abstract void onTouchEvent(MotionEvent event, float xF, float yF, int x, int y);

	public abstract void onTouchDownEvent(MotionEvent event, float xF, float yF, int x, int y);

	public abstract void onTouchUpEvent(MotionEvent event, float xF, float yF, int x, int y);

	public abstract void onTouchMoveEvent(MotionEvent event, float xF, float yF, int x, int y);
}
